﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using Microsoft.Phone;
using Sisocana.PhoneApp.MobileServices;

namespace Sisocana.PhoneApp.Views
{
    public partial class InfoPage : PhoneApplicationPage
    {
        private static bool _visited;
        CameraCaptureTask camTask;

        public InfoPage() {
            InitializeComponent();
            camTask = new CameraCaptureTask();
            camTask.Completed += new EventHandler<PhotoResult>(camTask_Completed);

            // En caso de que el usuario no haya especificado su posición global.
            if (HelpRequestForm.Location == null) {
                GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();
                HelpRequestForm.Location = myWatcher.Position.Location;
                if (HelpRequestForm.Location.IsUnknown) {
                    HelpRequestForm.Location.Latitude = -34.918605;
                    HelpRequestForm.Location.Longitude = -56.166863;
                }
            }
        }

        public static bool AlreadyVisited {
            get { return InfoPage._visited; }
            set { InfoPage._visited = value; }
        }

        private Visibility isEmpty(string fieldText) {
            return fieldText.Trim().Length == 0 ? Visibility.Visible : Visibility.Collapsed;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e) {
            lblRequiredName.Visibility = isEmpty(txtFirstName.Text);
            lblRequiredLastName.Visibility = isEmpty(txtLastName.Text);
            lblRequiredSituationDescription.Visibility = isEmpty(txtSituationDescription.Text);

            if (lblRequiredName.Visibility == Visibility.Visible) {
                txtFirstName.Focus(); return;
            }

            if (lblRequiredLastName.Visibility == Visibility.Visible) {
                txtLastName.Focus(); return;
            }

            if (lblRequiredSituationDescription.Visibility == Visibility.Visible) {
                txtSituationDescription.Focus(); return;
            }

            HelpRequestForm.FirstName = txtFirstName.Text;
            HelpRequestForm.LastName = txtLastName.Text;
            HelpRequestForm.SituationDescription = txtSituationDescription.Text;

            (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/LastPage.xaml", UriKind.Relative));
        }

        private void btnMap_Click(object sender, RoutedEventArgs e) {
            (App.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Views/MapPage.xaml", UriKind.Relative));
        }

        private void btnCamera_Click(object sender, RoutedEventArgs e) 
        {
            camTask.Show();
        }

        void camTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                DataImagen imagenPersona = new DataImagen();
                imagenPersona.imgExt = ".jpeg";
                imagenPersona.imgNombre = e.OriginalFileName;
                long streamLength = e.ChosenPhoto.Length;
                imagenPersona.imgByte = new byte[streamLength];
                e.ChosenPhoto.Read(imagenPersona.imgByte, 0, (int)streamLength);
                HelpRequestForm.ImagenPersona = imagenPersona;
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (AlreadyVisited) {
                NavigationService.GoBack();
                AlreadyVisited = false;
            }
        }
    }
}